package org.ellisande.grid.tags;

import java.util.EnumSet;

import javax.servlet.jsp.JspWriter;

import org.ellisande.grid.tags.support.TagBuilder;
import org.ellisande.tld.annotations.Attribute;
import org.ellisande.tld.annotations.Description;
import org.ellisande.tld.annotations.Info;
import org.ellisande.tld.annotations.Tag;

@Tag
@Info("Defines a closable alert area. &lt;h4 class=&quot;alert-header&quot; &gt; can be used to define an alert header.")
public class AlertTag extends AbstractTag {

	private static final long serialVersionUID = -5147350271190511204L;

	private static enum Type {
		WARNING(""), ERROR("alert-error"), SUCCESS("alert-success"), INFO("alert-info");
		
		private String type;
		
		private Type(String type){
			this.type = type;
		}
		
		public String toString(){
			return type;
		}
	}
	
	@Attribute
	@Description("Defines the type of alert. Allowed values: warning | error | success | info. Default value: warning.")
	private Type type = Type.WARNING;
	
	@Attribute
	@Description("Defines whether or not this alert will be contained in a block. Adds padding to the alert. Allowed values: true | false. Default: false.")
	private boolean block = false;
	
	private static final String href="#";
	
	public void setType(String type){
		for(Type current : EnumSet.allOf(Type.class)){
			if(current.name().equalsIgnoreCase(type)){
				this.type = current;
				return;
			}
		}
	}
	
	public void setBlock(String block){
		this.block = Boolean.parseBoolean(block);
	}

	@Override
	public void startTag() throws Exception {
		TagBuilder div = createTagBuilder("div");
		TagBuilder button = createTagBuilder("button");
		div.addStyle("alert");
		div.addStyle(type.toString());
		if(block){
			div.addStyle("alert-block");
		}
		button.addStyle("close");
		button.addAttribute("data-dismiss", "alert");
		button.addAttribute("href",href);
		
		JspWriter out = pageContext.getOut();
		out.write(div.compileStartTag());
		out.write(button.compileStartTag()+"x"+button.compileEndTag());

	}

	@Override
	public void endTag() throws Exception {
		JspWriter out = pageContext.getOut();
		out.write("</div>");
	}

}
